﻿using System;
using SharePhone.Common;

namespace SharePhone.Core
{
    public enum CacheStatus
    {
        Fresh,
        Old
    }

    /// <summary>
    /// Class containing helpers for stuff like caching
    /// </summary>
    public class FrameworkObject
    {
        /// <summary>
        /// Get/set the time when this object was last refreshed from source
        /// </summary>
        [CoreField(PopulateRule = CorePopulateRule.None)]
        public DateTime LastRefreshTimeStamp { get; set; }

        internal CacheStatus GetCacheStatus(ContextSettings settings)
        {
            var minutesSinceLastRefresh = (int) (DateTime.Now - LastRefreshTimeStamp).TotalMinutes;
            if (minutesSinceLastRefresh > settings.CacheTimeout)
                return CacheStatus.Old;
            return CacheStatus.Fresh;
        }

        public void InvalidateCache()
        {
            LastRefreshTimeStamp = DateTime.MinValue;
        }
    }
}